package com.bw.home.adpter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bw.home.databinding.ItemSimpleInfoBinding
import com.bw.home.entity.SimpleVideoEntity

class SimpleVideoAdapter(val context: Context, var list: List<SimpleVideoEntity>) :
    RecyclerView.Adapter<SimpleVideoAdapter.SimpleViewHolder>() {

    class SimpleViewHolder(val binding: ItemSimpleInfoBinding) :
        RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SimpleViewHolder {
        val view = ItemSimpleInfoBinding.inflate(LayoutInflater.from(context), parent, false)
        return SimpleViewHolder(view)
    }

    override fun onBindViewHolder(holder: SimpleViewHolder, position: Int) {
        val entity = list.get(position)
        Glide.with(context).load(entity.avatar_url).into(holder.binding.itemSimpleHeadIv)
        Glide.with(context).load(entity.videomainimag).into(holder.binding.itemSimpleBack)
        holder.binding.itemSimpleName.text = entity.name
        holder.binding.itemSimpleDate.text = entity.ctime
        holder.binding.itemSimpleTitle.text = entity.title
        holder.binding.itemSimpleStart.setOnClickListener {
            if (playOnClickListener != null) {
                playOnClickListener.onClick(entity)
            }
        }

        holder.binding.itemSimpleShare.setOnClickListener {
            if (shareOnClickListener != null) {
                shareOnClickListener.onClick(entity)
            }
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }


    //定义接口
    interface PlayOnClickListener {
        fun onClick(entity: SimpleVideoEntity)
    }

    //声明接口变量
    private lateinit var playOnClickListener: PlayOnClickListener

    //对外提供接口
    fun setPlayOnClickListener(playOnClickListener: PlayOnClickListener) {
        this.playOnClickListener = playOnClickListener
    }


    //定义接口
    interface ShareOnClickListener {
        fun onClick(entity: SimpleVideoEntity)
    }

    //声明接口变量
    private lateinit var shareOnClickListener: ShareOnClickListener

    //对外提供接口
    fun setShareOnClickListener(shareOnClickListener: ShareOnClickListener) {
        this.shareOnClickListener = shareOnClickListener
    }
}